package com.simon.mynotes;

import java.util.ArrayList;
import java.util.List;

import com.simon.dao.NoteRepository;
import com.simon.notes.model.NotesModel;
import com.simon.preference.MyNotesPreference;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MyNotes extends ActionBarActivity {

	private EditText searchField;
	// private Button searchButton;
	private Button addButton;
	private ListView notesListl;
	private ArrayAdapter<String> adapter;

	private final static int REQUEST_CODE = 1;

	// Database
	private NoteRepository repos;

	private List<NotesModel> notesList = new ArrayList<NotesModel>();

	private List<NotesModel> actualNotesList = new ArrayList<NotesModel>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.my_notes);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}

		searchField = (EditText) findViewById(R.id.SearchField);

		addButton = (Button) findViewById(R.id.AddButton);
		notesListl = (ListView) findViewById(R.id.NotesList);
		repos = new NoteRepository(this);

		searchField.addTextChangedListener(new MyTextWatcher());
		notesListl.setOnItemClickListener(new MyOnItemClickListener());
		notesListl.setOnItemLongClickListener(new MyOnItemLongClickListener());

		updateList();
	}

	@Override
	protected void onResume() {
		super.onResume();
		updateList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_notes, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			Intent i = new Intent(this, MyNotesPreference.class);
			startActivity(i);
			break;
		}
		return true;

	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_my_notes,
					container, false);
			return rootView;
		}
	}

	public void searchNotes(View v) {

		search(searchField.getText().toString());
	}

	public void addNote(View v) {
		Intent i = new Intent(this, AddNote.class);
		startActivity(i);
	}

	private void search(String topic) {
		notesList = repos.findByTopic(topic);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, notesTopic(notesList));
		notesListl.setAdapter(adapter);
	}

	private List<String> notesTopic(List<NotesModel> notes) {
		List<String> topicList = new ArrayList<String>();
		for (NotesModel model : notes) {
			topicList.add(model.getTopic());
			actualNotesList.add(model);
		}
		return topicList;
	}

	private class MyTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			search(searchField.getText().toString());
		}

	}

	private class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			NotesModel tmpModel = new NotesModel();
			tmpModel = actualNotesList.get(arg2);

			Intent i = new Intent(MyNotes.this, AddNote.class);
			i.putExtra("model", tmpModel);

			startActivityForResult(i, REQUEST_CODE);
		}
	}

	private class MyOnItemLongClickListener implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View v, int index,
				long arg3) {
			repos.delete(actualNotesList.get(index));
			updateList();
			return true;
		}
	}

	private void updateList() {
		notesList = repos.getAll();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, notesTopic(notesList));
		notesListl.setAdapter(adapter);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("returnModel")) {
				NotesModel toSave = new NotesModel();

				toSave = (NotesModel) data.getSerializableExtra("returnModel");
				repos.update(toSave);
				updateList();
			}
		}
	}

}
